package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.impl.EmployeeQuery;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"employee:view","员工页面"},logical= Logical.OR)
    public String view() {
        System.out.println((":)"));
        return "employee";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(EmployeeQuery qo) {
        return employeeService.query(qo);
    }

    @RequestMapping("validateUsername")
    @ResponseBody
    public boolean validateUsername(String username, Long id) {
        Employee employee = employeeService.getByUsername(username);
        return employee == null || employee.getId() == id;
    }
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"employee:save","保存员工"},logical= Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Employee employee){
        try {
            employeeService.saveOrUpdate(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }
    @RequestMapping("changeState")
    @RequiresPermissions(value = {"employee:change","更改员工状态"},logical= Logical.OR)
    @ResponseBody
    public JSONResult changeState(Long id){
        try {
            employeeService.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"状态更新失败");
        }
        return new JSONResult(true,"状态更新成功");
    }
    @RequestMapping("rsetPassword")
    @RequiresPermissions(value = {"employee:reset","重置密码"},logical= Logical.OR)
    @ResponseBody
    public JSONResult rsetPassword (Long id, String newpassword){
        try {
            employeeService.rsetPassword(id , newpassword);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"密码重置失败请重新执行");
        }
        return new JSONResult(true,"密码重置成功请妥善保管");
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //文件下载响应头
        response.setHeader("Content-disposition", "attachment;filename=employee.xls");
        //创建excel文件(workbook)
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("day01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        //查询员工数据
        List<Employee> list = employeeService.selectAll();
        for (int i = 0; i < list.size(); i++) {
            Employee employee = list.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getRealname());
            row.createCell(2).setCellValue(employee.getTel());
        }
        //写入数据
        workbook.write(response.getOutputStream());
        workbook.close();
    }
    @RequestMapping("importXls")
    public void importXls(MultipartFile file) throws IOException {
        //读取excel文件(workbook)
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        HSSFSheet sheet = workbook.getSheet("day01");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i < lastRowNum; i++) {
            Employee employee = new Employee();
            //获取每一行的数据
            HSSFRow row = sheet.getRow(i+1);
            if(row!=null&&row.getCell(0)!=null) {
                employee.setUsername(row.getCell(0).getStringCellValue());
                employee.setRealname(row.getCell(1).getStringCellValue());
                employee.setTel(row.getCell(2).getStringCellValue());
                employeeService.saveOrUpdate(employee);
            }
        }
        workbook.close();
    }
    @RequestMapping("getRoleIdByEmployeeId")
    @ResponseBody
    public List<Long> getRoleIdByEmployeeId(Long employeeId){
        return employeeService.getRoleIdByEmployeeId(employeeId);
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Employee> selectAll(){
        return employeeService.selectAll();
    }
}
